package com.subang.function.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.subang.framework.web.service.UserIntegralService;
import com.subang.common.constant.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.subang.common.annotation.Log;
import com.subang.common.core.controller.BaseController;
import com.subang.common.core.domain.AjaxResult;
import com.subang.common.enums.BusinessType;
import com.subang.function.domain.SbApplication;
import com.subang.function.service.ISbApplicationService;
import com.subang.common.utils.poi.ExcelUtil;
import com.subang.common.core.page.TableDataInfo;

/**
 * 岗位筛选Controller
 * 
 * @author subei
 * @date 2025-10-20
 */
@RestController
@RequestMapping("/function/application")
public class SbApplicationController extends BaseController
{
    @Autowired
    private ISbApplicationService sbApplicationService;

    @Autowired
    private UserIntegralService userIntegralService;

    /**
     * 查询岗位筛选列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SbApplication sbApplication)
    {
        startPage();
        List<SbApplication> list = sbApplicationService.selectSbApplicationList(sbApplication);
        
        // 查询成功后更新用户积分
        if (list != null && !list.isEmpty())
        {
            AjaxResult result = userIntegralService.updateUserIntegral("findJob");
            // 如果积分更新失败（如积分不足），返回错误提示
            if (result.isError())
            {
                TableDataInfo rspData = new TableDataInfo();
                rspData.setCode(HttpStatus.ERROR);
                rspData.setMsg("用户积分不足，无法完成操作，请努力赚积分！");
                return rspData;
            }
        }

        return getDataTable(list);
    }

    /**
     * 导出岗位筛选列表
     */
    @PreAuthorize("@ss.hasPermi('function:application:export')")
    @Log(title = "岗位筛选", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SbApplication sbApplication)
    {
        List<SbApplication> list = sbApplicationService.selectSbApplicationList(sbApplication);
        ExcelUtil<SbApplication> util = new ExcelUtil<SbApplication>(SbApplication.class);
        util.exportExcel(response, list, "岗位筛选数据");
    }

    /**
     * 获取岗位筛选详细信息
     */
    @GetMapping(value = "/{recruitmentYear}")
    public AjaxResult getInfo(@PathVariable("recruitmentYear") String recruitmentYear)
    {
        return success(sbApplicationService.selectSbApplicationByRecruitmentYear(recruitmentYear));
    }

    /**
     * 新增岗位筛选
     */
    @PreAuthorize("@ss.hasPermi('function:application:add')")
    @Log(title = "岗位筛选", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SbApplication sbApplication)
    {
        return toAjax(sbApplicationService.insertSbApplication(sbApplication));
    }

    /**
     * 修改岗位筛选
     */
    @PreAuthorize("@ss.hasPermi('function:application:edit')")
    @Log(title = "岗位筛选", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SbApplication sbApplication)
    {
        return toAjax(sbApplicationService.updateSbApplication(sbApplication));
    }

    /**
     * 删除岗位筛选
     */
    @PreAuthorize("@ss.hasPermi('function:application:remove')")
    @Log(title = "岗位筛选", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recruitmentYears}")
    public AjaxResult remove(@PathVariable String[] recruitmentYears)
    {
        return toAjax(sbApplicationService.deleteSbApplicationByRecruitmentYears(recruitmentYears));
    }
}
